﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ChromaticQFX
{
    public partial class ViewTransactions : Form
    {
        private bool IsLoading = false;

        #region Common Form Methods

        public ViewTransactions()
        {
            InitializeComponent();
        }

        private void ViewTransactions_Load(object sender, EventArgs e)
        {
            lstTransactions.DataSource = MainForm.history.FilterByCategory("");

            ddlCategories.DataSource = MainForm.history.Categories;

            // For 1024x600 netbooks, go ahead and maximize screen
            if (SystemInformation.VirtualScreen.Width == 1024 && SystemInformation.VirtualScreen.Height == 600)
                WindowState = FormWindowState.Maximized;
        }

        private void ClearForm()
        {
            txtFitID.Text = "";
            txtMemo.Text = "";
            txtName.Text = "";
            txtTransactionAmount.Text = "";
            txtType.Text = "";
            dtpDatePosted.Value = DateTime.Today;
        }

        #endregion

        #region Transaction List Events/Methods

        private void lstTransactions_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lstTransactions.SelectedItem == null) return;

            QFXTransaction trans = (QFXTransaction)lstTransactions.SelectedItem;

            ClearForm();

            txtName.Text = trans.Name;
            txtMemo.Text = trans.Memo;
            txtType.Text = trans.TransactionType;
            txtFitID.Text = trans.FitID;
            txtTransactionAmount.Text = trans.TransactionAmount.ToString();
            dtpDatePosted.Value = trans.DatePosted;
        }

        private void lstTransactions_DoubleClick(object sender, EventArgs e)
        {
            if (lstTransactions.SelectedItem == null) return;

            QFXTransaction trans = (QFXTransaction) lstTransactions.SelectedItem;

            for (int idx = 0; idx < lstTransactions.Items.Count; idx++)
            {
                QFXTransaction transCursor = (QFXTransaction) lstTransactions.Items[idx];

                if (trans.Name == transCursor.Name && trans != transCursor)
                {
                    lstTransactions.SelectedIndices.Add(idx);
                }
            }
        }

        #endregion

        #region Category Management

        private void ddlCategories_SelectedIndexChanged(object sender, EventArgs e)
        {
            IsLoading = true;

            lstCategoryTrans.DataSource = null;
            lstCategoryTrans.DataSource = MainForm.history.FilterByCategory(ddlCategories.Text);

            IsLoading = false;
        }

        private void btnSetCategory_Click(object sender, EventArgs e)
        {
            if (lstTransactions.SelectedIndices.Count == 0) return;

            int TransIndex = lstTransactions.SelectedIndices[0];

            foreach (int transIndex in lstTransactions.SelectedIndices)
            {
                QFXTransaction trans = (QFXTransaction) lstTransactions.Items[transIndex];

                trans.Category = ddlCategories.Text;
            }

            lstTransactions.DataSource = null;
            lstTransactions.DataSource = MainForm.history.FilterByCategory("");

            lstCategoryTrans.DataSource = null;
            lstCategoryTrans.DataSource = MainForm.history.FilterByCategory(ddlCategories.Text);

            if (TransIndex < lstTransactions.Items.Count)
            {
                lstTransactions.SelectedIndices.Clear();
                lstTransactions.SelectedIndex = TransIndex;
            }

        }

        private void btnClearCategory_Click(object sender, EventArgs e)
        {
            if (lstCategoryTrans.SelectedItem == null) return;

            QFXTransaction trans = (QFXTransaction)lstCategoryTrans.SelectedItem;

            trans.Category = "";

            lstTransactions.DataSource = null;
            lstTransactions.DataSource = MainForm.history.FilterByCategory("");

            lstCategoryTrans.DataSource = null;
            lstCategoryTrans.DataSource = MainForm.history.FilterByCategory(ddlCategories.Text);

        }

        private void lstCategoryTrans_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (IsLoading) return;

            if (lstCategoryTrans.SelectedItem == null) return;

            QFXTransaction trans = (QFXTransaction)lstCategoryTrans.SelectedItem;

            ClearForm();

            txtName.Text = trans.Name;
            txtMemo.Text = trans.Memo;
            txtType.Text = trans.TransactionType;
            txtFitID.Text = trans.FitID;
            txtTransactionAmount.Text = trans.TransactionAmount.ToString();
            dtpDatePosted.Value = trans.DatePosted;

        }

        #endregion

        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }

    }
}
